package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hss01248.dialog.StyledDialog;
import com.hss01248.dialog.interfaces.MyDialogListener;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.di.component.DaggerHandOverComponent;
import com.lonwin.inspectionmanagement.mvp.contract.HandOverContract;
import com.lonwin.inspectionmanagement.mvp.model.api.AppConstant;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ResQdetailBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.UserBean;
import com.lonwin.inspectionmanagement.mvp.presenter.HandOverPresenter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.UserAdapter;
import com.lonwin.inspectionmanagement.widget.dialog.LoadingDailog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/25/2018 19:11
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HandOverActivity extends BaseActivity<HandOverPresenter> implements HandOverContract.View {
    @BindView(R2.id.rv_userinfo)
    RecyclerView rvuserinfo;
    @BindView(R2.id.back_icon)
    ImageView backIcon;
    @BindView(R2.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R2.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R2.id.back_right_icon)
    TextView backRightIcon;
    @BindView(R2.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.newList_SearchEditText)
    EditText newListSearchEditText;
    @BindView(R2.id.tv_confirm)
    TextView tvConfirm;
    private LoadingDailog loadingDailog;
    private  UserAdapter muserAdapter;
    ResQdetailBean.DataBean dataBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHandOverComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_hand_over; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadingDailog = new LoadingDailog.Builder(this).setMessage("数据获取中...").create();
        mPresenter.initData();
        mPresenter.getSelectPerson(getMapparmar(null));
        newListSearchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             if (s!=null&&s.length()>0)
             {
                 mPresenter.getSelectPerson(getMapparmar(s.toString()));
             }
            }
        });
    }

    /**
     * 封装请求参数
     *
     * @param name 搜索的时候使用的，不是搜索的时候就不用
     *             gpsId=238800 登陆账号ID
     *             groupId=14152  选择的河湖
     *             adcd=532900000000000 登陆账号行政区划
     *             roleCode=ROLE_HZB  登陆账号角色
     *             name=大理  关键字查询
     * @return
     */
    public Map<String, Object> getMapparmar(String name) {
        Map<String, Object> maps = new HashMap<>();
        dataBean = (ResQdetailBean.DataBean) getIntent().getSerializableExtra(AppConstants.EXTRA_DATA);
        maps.put("gpsId", mPresenter.getConfigBean().getGpsId());
        if (dataBean!=null){
            maps.put("groupId",dataBean.getGroupid());
        }
        maps.put("adcd", mPresenter.getConfigBean().getAdcd());
        maps.put("roleCode", mPresenter.getConfigBean().getCurrentRole().getRoleCode());
        if (name != null) {
            maps.put("name", name);
        }
        return maps;
    }

    @Override
    public void showLoading() {
        loadingDailog.show();
    }

    @Override
    public void hideLoading() {
        loadingDailog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R2.id.tv_confirm)
    public void onBindWeChatClick(View v) {
        StyledDialog.buildIosAlert("交办问题！", "确定交办该问题？", new MyDialogListener() {
            @Override
            public void onFirst() {
                mPresenter.handOverto(dataBean);
            }

            @Override
            public void onSecond() {
                StyledDialog.dismiss();
            }
        }).show();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void serUserAdapter(UserAdapter userAdapter) {
        muserAdapter=userAdapter;
        rvuserinfo.setLayoutManager(new LinearLayoutManager(this));
        userAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        rvuserinfo.setAdapter(userAdapter);
    }

    @Override
    public void getSelectPersonSuccess(List<UserBean.DatasBean> datasBeans) {
        muserAdapter.setNewData(datasBeans);
    }

    @Override
    public List<UserBean.DatasBean> getSelectedUser() {
        if(muserAdapter!=null){
            return muserAdapter.getSelectedUser();
        }
        return new ArrayList<UserBean.DatasBean>();
    }

    @Override
    public void handOverSuccess(String msg) {
        showMessage(msg);
        Intent intent =new Intent();
         setResult(RESULT_OK,intent);
        killMyself();
    }

    @Override
    public void handOverFail(String msg) {
        showMessage(msg);
    }
}
